package com.example.farmrental.controller;

import com.example.farmrental.dto.ApiResponse;
import com.example.farmrental.entity.Machinery;
import com.example.farmrental.service.MachineryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 农机管理控制器
 */
@RestController
@RequestMapping("/api/machinery")
@CrossOrigin(origins = "*") // 允许跨域请求
public class MachineryController {
    
    @Autowired
    private MachineryService machineryService;
    
    /**
     * 获取所有农机列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<Machinery>> getAllMachinery() {
        List<Machinery> machineryList = machineryService.getAllMachinery();
        return ResponseEntity.ok(machineryList);
    }
    
    /**
     * 根据ID获取农机详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Machinery> getMachineryById(@PathVariable Long id) {
        Optional<Machinery> machinery = machineryService.getMachineryById(id);
        if (machinery.isPresent()) {
            return ResponseEntity.ok(machinery.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 新增农机
     */
    @PostMapping
    public ResponseEntity<Machinery> createMachinery(@RequestBody Machinery machinery) {
        Machinery savedMachinery = machineryService.saveMachinery(machinery);
        return ResponseEntity.ok(savedMachinery);
    }
    
    /**
     * 更新农机信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Machinery> updateMachinery(@PathVariable Long id, @RequestBody Machinery machinery) {
        Optional<Machinery> existingMachinery = machineryService.getMachineryById(id);
        if (existingMachinery.isPresent()) {
            machinery.setId(id);
            Machinery updatedMachinery = machineryService.saveMachinery(machinery);
            return ResponseEntity.ok(updatedMachinery);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 删除农机
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteMachinery(@PathVariable Long id) {
        Optional<Machinery> machinery = machineryService.getMachineryById(id);
        if (machinery.isPresent()) {
            machineryService.deleteMachinery(id);
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 根据分类查询农机
     */
    @GetMapping("/category/{category}")
    public ResponseEntity<List<Machinery>> getMachineryByCategory(@PathVariable String category) {
        List<Machinery> machineryList = machineryService.getMachineryByCategory(category);
        return ResponseEntity.ok(machineryList);
    }
    
    /**
     * 根据状态查询农机
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Machinery>> getMachineryByStatus(@PathVariable String status) {
        List<Machinery> machineryList = machineryService.getMachineryByStatus(status);
        return ResponseEntity.ok(machineryList);
    }
    
    /**
     * 根据名称搜索农机
     */
    @GetMapping("/search")
    public ResponseEntity<List<Machinery>> searchMachinery(@RequestParam String name) {
        List<Machinery> machineryList = machineryService.searchMachineryByName(name);
        return ResponseEntity.ok(machineryList);
    }
    
    /**
     * 获取所有农机分类列表
     */
    @GetMapping("/categories")
    public ResponseEntity<List<String>> getAllCategories() {
        List<String> categories = machineryService.getAllCategories();
        return ResponseEntity.ok(categories);
    }
    
    /**
     * 获取可用的农机列表
     */
    @GetMapping("/available")
    public ResponseEntity<ApiResponse<List<Machinery>>> getAvailableMachinery() {
        try {
            List<Machinery> availableMachinery = machineryService.getMachineryByStatus("起租");
            return ResponseEntity.ok(ApiResponse.success("获取可用农机列表成功", availableMachinery));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取可用农机列表失败: " + e.getMessage()));
        }
    }
}